/*
 * i2c_perripherals.h
 *
 *  Created on: 21.03.2012
 *      Author: Roman Leitner
 */

#include "../driver/i2c.h"

#ifndef I2C_PERRIPHERALS_H_
#define I2C_PERRIPHERALS_H_

typedef enum
{
  I2C_ERROR_OK = 0,               // Everything executed normally
  I2C_ERROR_I2C_INIT,             // Unable to initialise I2C
  I2C_ERROR_I2C_BUSY,             // I2C already in use
  I2C_ERROR_I2C_BUFFEROVERFLOW,   // I2C Buffer is too small
  I2C_ERROR_RTC_LOWBATT,          // RTC battery low ... value may not be accurate
  I2C_ERROR_LAST
}
i2c_Error_t;

extern i2c_Error_t I2C_Write8 (uint8_t address, uint8_t reg, uint32_t value);
extern i2c_Error_t I2C_ReadBuffer(uint8_t address, uint8_t reg, uint8_t *buffer, uint32_t len);

extern i2c_Error_t I2C_Send_Byte (uint8_t address, uint32_t value);
extern i2c_Error_t I2C_Receive_Byte(uint8_t address,uint8_t *buffer);


#define DS1307_RTC_ADDRESS	   	0xd0   // b'1101000(RW)
#define DS1307_REG_RTC_SECOND  	0x00
#define DS1307_REG_RTC_MINUTE 	0x01
#define DS1307_REG_RTC_HOUR 	0x02
#define DS1307_REG_RTC_DAYOFWEEK	0x03
#define DS1307_REG_RTC_DATE		0x04
#define DS1307_REG_RTC_MONTH	0x05
#define DS1307_REG_RTC_YEAR 	0x06
#define DS1307_REG_RTC_CONTROL 	0x07

#define DS1307_HR_MILITARY       (0<<6)    // 0 = 24-Hour, 1 = 12-Hour

#define DS1307_MONDAY 		0x01
#define DS1307_TUESDAY		0x02
#define DS1307_WEDNESDAY 	0x03
#define DS1307_THURSDAY		0x04
#define DS1307_FRIDAY   	0x05
#define DS1307_SATURDAY 	0x06
#define DS1307_SUNDAY   	0x07

typedef enum
{
  DS1307_ERROR_OK = 0,               // Everything executed normally
  DS1307_ERROR_I2C_INIT,             // Unable to initialise I2C
  DS1307_ERROR_I2C_BUSY,             // I2C already in use
  DS1307_ERROR_I2C_BUFFEROVERFLOW,   // I2C Buffer is too small
  DS1307_ERROR_RTC_LOWBATT,          // RTC battery low ... value may not be accurate
  DS1307_ERROR_LAST
}
DS1307Error_t;

typedef struct /* Clock structure */
{
  uint8_t hour;
  uint8_t minute;
  uint8_t second;
  uint8_t dayofweek;
  uint8_t day;
  uint8_t month;
  uint8_t year;
  uint8_t status;
  uint8_t interrupt;
} RTC_time_t;

/*
extern uint32_t DS1307_readTime_seconds( void  );
extern uint32_t DS1307_readTime_minutes( void  );
extern uint32_t DS1307_readTime_hours( void  );
extern uint32_t DS1307_readTime_weekday( void  );
extern uint32_t DS1307_readTime_date( void  );
extern uint32_t DS1307_readTime_month( void  );
extern uint32_t DS1307_readTime_year( void  );
*/

extern RTC_time_t  DS1307_readTime (void);
extern uint32_t    DS1307_writeTime (RTC_time_t time);

extern DS1307Error_t DS1307Init(void);
extern DS1307Error_t DS1307GetTime(RTC_time_t *time);
extern DS1307Error_t DS1307SetTime(uint8_t control, uint8_t dayofweek, uint8_t day, uint8_t month, uint8_t year, uint8_t hour, uint8_t minute, uint8_t second);



#define TSL2550_LIGHT_ADDRESS	        0x72   // b'0111-001(RW)
#define TSL2550_LIGHT_CMD_POWER_DOWN		0x00
#define TSL2550_LIGHT_CMD_POWER_UP		0x03
#define TSL2550_LIGHT_CMD_STANDARD_RANGE		0x18
#define TSL2550_LIGHT_CMD_EXTENDED_RANGE		0x1d
#define TSL2550_LIGHT_CMD_READ_ADC0		0x43
#define TSL2550_LIGHT_CMD_READ_ADC1		0x83

// PCA9632 PWM i2C leddriver
#define PCA9632_ADDRESS           0xa4   // b'1100-010(RW)
#define PCA9632_ADDRESS_allCall   0xE0   //
#define PCA9632_ADDRESS_SWRST     0x06   //



typedef enum
{
  TSL2550_ERROR_OK = 0,               // Everything executed normally
  TSL2550_ERROR_POWER_UP_FAIL,        // Unable to power up device
  TSL2550_ERROR_I2C_BUFFEROVERFLOW,   // buffer length to small
  TSL2550_ERROR_READVALUE_NOTCORRECT, // readback value wrong
  TSL2550_ERROR_BROADBAND_INVALID,
  TSL2550_ERROR_IR_INVALID,
  TSL2550_ERROR_LAST
}
TSL2550Error_t;


extern uint32_t TSL2550GetLux( );
extern TSL2550Error_t TSL2550GetLum(uint8_t *broad, uint8_t *ir);
extern TSL2550Error_t TSL2550_init( void );
extern uint32_t TSL2550CalcLux(uint8_t broadband, uint8_t ir);

extern uint32_t TSL2550_rdADC( void  );


#define NE1617_TEMPSENS_ADDRESS 0x30  // ADD0,ADD1 tied to 0

#endif /* I2C_PERRIPHERALS_H_ */
